<?php
include_once 'module/controls.php';
include_once 'js/ckeditor/ckeditor.php';
include_once 'js/ckfinder/ckfinder.php';
include_once 'model/board/process.php';
include_once 'model/page/process.php';
include_once 'model/tags/process.php';
$board = new boards();
$page = new pages();
$tags = new tags();
$c = new controls();

$fillAll = $board->FillAll();
$fillOne = $page->FillOne($_GET);
$findAllTags = $tags->FillAll();
?>
<div id="content">
    <div class="content_block">
        <h2 class="jquery_tab_title"><?php echo $phrase["manager_category"] ?></h2>
        <form method="post" action="<?= $url["home"] ?>/menu/pageprocess/t/2" enctype="multipart/form-data">
            <p>
                <?php echo $c->get_textHidden("id", $fillOne["page_id"]) ?>
                <?php echo $c->get_label($phrase["page_title"]) ?>
                <?php echo $c->get_text("pagetitle", $fillOne["page_title"]) ?>
            </p>

            <p>
                <?php echo $c->get_label($phrase["page_summary"]) ?>
                <?php echo $c->get_textLager("pagesummary", $fillOne["page_summary"]) ?>
            </p>

            <p>
                <?php echo $c->get_label($phrase["page_image"]) ?>
                <?php echo $c->get_textReadOnly("pageimage", $fillOne["page_image"]) ?>
                <input class="button" type="button" value="Browser" onclick="BrowseServer();" />
            </p>

            <p>
                <?php echo $c->get_label($phrase["page_content"]) ?>
                <?php echo $c->get_textarea("pagecontent", $fillOne["page_content"]) ?>
            </p>

            <p>
                <?php echo $c->get_label($phrase["page_hotnews"]) ?>
                <?php
                if ($fillOne["page_hotnews"] == 1) {
                    echo $c->get_checkbox("pagehotnews", "1", "checked");
                } else {
                    echo $c->get_checkbox("pagehotnews", "1");
                }
                ?>
            </p>

            <p>
                <?php echo $c->get_label($phrase["page_showhome"]) ?>
                <?php
                if ($fillOne["page_showhome"] == 1) {
                    echo $c->get_checkbox("pageshowhome", "1", "checked");
                } else {
                    echo $c->get_checkbox("pageshowhome", "1");
                }
                ?>
            </p>
            
            <p>
                <?php echo $c->get_label($phrase["board_name"]) ?>
                <select name="board_name" id="cate_name" >
                    <?php
                    while ($rows = mysql_fetch_array($fillAll)) {
                        ?>
                        <option value="<?= $rows["board_id"] ?>" <?php if ($fillOne["board_id"] == $rows["board_id"]) { ?>selected='selected'<?php } ?>><?= $rows["board_name"] ?></option>
                    <?php } ?>
                </select>
            </p>

            <div class="tags"> 
                <div class="content-box box2">
                    <p>
                        <?php echo $c->get_label($phrase["page_tag"]) ?>
                        <?php echo $c->get_text("pagetag") ?>
                        <?php echo $c->get_textHidden("tag_data", $fillOne["page_tag"] . ",") ?>
                        <input class="button" type="button" value="Thêm" id="addTag" />                        
                    </p>
                    <p>
                        <?php echo $phrase["note"] ?>
                    </p>

                    <p id="show-data">
                        <?php
                        $tags = explode(",", $fillOne["page_tag"]);
                        foreach ($tags as $key => $_tags) {
                            ?>
                            <span class="noteleft" id="tags-<?php echo $key ?>">
                                <a href="javascript:void(0)" class="remove" onclick="updateTag()">
                                    <?php echo $_tags; ?>
                                </a>
                            </span>
                        <?php } ?>
                    </p>
                    <p style="float: left">
                        <a class="link1" href="javascript:void(0)">Click show more tags</a>
                    </p>
                    <br/>
                    <p class="show-tags" id="show-tags">
                        <?php while($rows = mysql_fetch_array($findAllTags)) : ?>
                            <span class="noteleft1" id="tags<?php echo $rows["tag_id"] ?>">
                                <a class="link1" href="javascript:void(0)" onclick="addTags(<?php echo $rows['tag_id'] ?>)"><?php echo $rows["tag_name"] ?></a>
                                <?php echo $c->get_textHidden("tags_{$rows["tag_id"]}", $rows["tag_name"]) ?>
                            </span>
                        <?php endwhile; ?>
                    </p>
                </div>
                
                <div class="content-box box2">
                    <p>
                        <?php echo $c->get_label($phrase["seo_title"]) ?>
                        <?php echo $c->get_text("seo_title", $fillOne["seo_title"]) ?>
                        <span class="error1" id="errTitle" style="display: none">Tiêu đề không được để trống hoặc không lớn hơn quá 70 ký tự.</span>
                    </p>
                    <p>
                        <?php echo $c->get_label($phrase["seo_description"]) ?>
                        <?php echo $c->get_text("seo_description", $fillOne["seo_description"]) ?>
                        <p id="errSummary" style="display: none">Nội dung không được để trống hoặc không lớn hơn quá 160 ký tự.</p>
                    </p>
                    <p>
                        <?php echo $c->get_label($phrase["seo_keyword"]) ?>
                        <?php echo $c->get_text("seo_keyword", $fillOne["seo_keyword"]) ?>
                    </p>
                </div>
            </div>

            <p>
                <?php echo $c->get_button("update", $phrase["update"]) ?>
            </p>
        </form>
    </div>
</div>

<?php
$ckeditor = new CKEditor();
$ckeditor->basePath = "{$url["home"]}/js/ckeditor/";
CKFinder::SetupCKEditor($ckeditor, "{$url['home']}/js/ckfinder/");
$ckeditor->replace("pagecontent");
?>
<script type="text/javascript">    
    function BrowseServer()
    {
        // You can use the "CKFinder" class to render CKFinder in a page:
        var finder = new CKFinder();
        finder.basePath = "/BRAVOLAW/admin/js/ckfinder/";	// The path for the installation of CKFinder (default = "/ckfinder/").
        finder.selectActionFunction = SetFileField;
        finder.popup();
    }

    // This is a sample function which is called when a file is selected in CKFinder.
    function SetFileField( fileUrl )
    {
        document.getElementById( 'pageimage' ).value = fileUrl;
    }
    
    $('#update').click(page_check);
    $('.link1').click(function(){
        $('#show-tags').show();
    });
    
    $("#addTag").bind("click",function(){
        var str = $('#pagetag').val().split(",");
        var strTag = $('#tag_data').val().split(",");
        var flag = false;
        $.each(str, function(i){
            $.each(strTag, function(index){
                if(str[i] != strTag[index]){
                    flag = true;
                }else{
                    flag = false;
                    return false;
                }
            });
            if(str[i] != "" && flag == true){
                $('#show-data').append("<span class='noteleft' id='tags-"+$('span.noteleft').length+"'><a href='javascript:void(0)' class='remove' onclick='updateTag()'>"+str[i]+"</a></span>");
                $('#tag_data').val($('#tag_data').val()+str[i]+",");
            }
        });
    });
    
    function addTags(item){
        var strTag = $('#tag_data').val().split(",");
        var strTagAll = $('#tags_'+item).val();
        var flag = false;
        console.log(strTagAll);
        $.each(strTag, function(index){
            if(strTagAll != strTag[index]){
                flag = true;
            }else{
                flag = false;
                return false;
            }
        });
        
        if(strTagAll != "" && flag == true){
            $('#show-data').append("<span class='noteleft' id='tags-"+$('span.noteleft').length+"'><a href='javascript:void(0)' class='remove' onclick='updateTag()'>"+strTagAll+"</a></span>");
            $('#tag_data').val($('#tag_data').val()+strTagAll+",");
        }
    }
    
    function updateTag(){
        $('span.noteleft').each(function(index){
            
            $("#tags-"+index).bind("click", function(){
                var removeTag = $('#tag_data').val().split(",");
                var data = "";
                var str = $.trim($(this).text());
                $.each(removeTag, function(i){
                    if(removeTag[i] == str){
                        removeTag.splice(i,1);
                    }
                    if(removeTag[i] != "" && removeTag[i] != undefined){
                        data += removeTag[i]+",";
                    }
                    $('#tag_data').val(data);
                });
                var addTag = $('#tag_data').val().split(",");
                $("span.noteleft").remove();
                $.each(addTag, function(ai){
                    if(addTag[ai] != ""){
                        $('#show-data').append("<span class='noteleft' id='tags-"+ai+"'><a href='javascript:void(0)' class='remove' onclick='updateTag()'>"+addTag[ai]+"</a></span>");
                    }
                });
                //                $(this).remove();
            });
            
        });
    }
    
</script>